/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vzone;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import vzone.bank.Bank;
import vzone.community.Community;
import vzone.market.Market;
import vzone.market.Shop;
import vzone.market.ShopControl;
import vzone.restaurant.Restaurant;
import vzone.software.SoftSale;
import vzone.structs.BasePeople;
import vzone.structs.Billing;
import vzone.structs.Company;
import vzone.structs.Dish;
import vzone.structs.Goods;
import vzone.structs.OperatorCode;
import vzone.structs.SoftwareInfo;

/**
 *服务工人 多线程类
 * 
 * @author Kingpro
 */
public class VzoneWorker implements Runnable
{

    Socket socket;
    DataInputStream inputStream;
    DataOutputStream outputStream;
    OperatorCode oc;

    /**
     * 构造函数
     * @param skt 请求的Socket
     */
    public VzoneWorker(Socket skt)
    {
	socket = skt;
    }

    @Override
    public void run()
    {

	try
	{
	    inputStream = new DataInputStream(socket.getInputStream());
	    outputStream = new DataOutputStream(socket.getOutputStream());

	    int msgI = inputStream.readInt();
	    switch (msgI)
	    {
		case 10:	//登录
		    String nn = inputStream.readUTF();
		    System.out.println(nn);
		    String pwdS = inputStream.readUTF();
		    System.out.println(pwdS);
		    BasePeople people = new BasePeople();

		    oc = Community.getCommunity().Login(nn, pwdS, people);
		    if (oc == OperatorCode.SUCCESS)
		    {
			outputStream.writeUTF("1");
			outputStream.flush();
			outputStream.writeUTF(people.PeopleIdUuid.toString() + "|" + people.NameS + "|" + people.GenderB + "|" + Long.toString(people.BirthdayD) + "|" + people.PicI + "|" + people.accountIdUuid.toString());
			outputStream.flush();
		    }
		    else
		    {
			outputStream.writeUTF("0");
			outputStream.flush();
		    }
		    System.out.println(oc);
		    break;
		case 11:	//注册
		    BasePeople peopler = new BasePeople();
		    peopler.NameS = inputStream.readUTF();
		    peopler.NickName = inputStream.readUTF();
		    String pwdS1 = inputStream.readUTF();
		    peopler.GenderB = inputStream.readBoolean();
		    peopler.PicI = inputStream.readInt();
		    peopler.BirthdayD = Long.parseLong(inputStream.readUTF());
		    oc = Community.getCommunity().Register(peopler, pwdS1);
		    if (oc == OperatorCode.SUCCESS)
		    {
			outputStream.writeUTF("1");
			outputStream.flush();
		    }
		    else
		    {
			outputStream.writeUTF("0");
			outputStream.flush();
		    }
		    break;
		case 20:	//  获取账户余额
		    String accountIdS = inputStream.readUTF();
		    String pwdS12 = inputStream.readUTF();
		    vzone.tools.Double balanceD = new vzone.tools.Double(0);
		    oc = Bank.getBank().GetAccountBalance(UUID.fromString(accountIdS), pwdS12, balanceD);
		    if (oc == OperatorCode.SUCCESS)
		    {
			outputStream.writeUTF(Double.toString(balanceD.doubleValue()));
			outputStream.flush();
		    }
		    else
		    {
			outputStream.writeUTF("ERROR");
			outputStream.flush();
		    }
		    break;
		case 21:    //建立账户
		    String idS = inputStream.readUTF();
		    String pwdS123 = inputStream.readUTF();
		    BasePeople people1 = new BasePeople();
		    people1.PeopleIdUuid = UUID.fromString(idS);
		    try
		    {
			UUID id = Bank.getBank().CreateAccount(people1, pwdS123);
			outputStream.writeUTF("1");
			outputStream.flush();
			outputStream.writeUTF(idS.toString());
			outputStream.flush();
		    }
		    catch (Exception e)
		    {
			outputStream.writeUTF("0");
			outputStream.flush();
		    }
		    break;
		case 22:	//转账
		    String fromac = inputStream.readUTF();
		    String toacS = inputStream.readUTF();
		    double balD = Double.valueOf(inputStream.readUTF());
		    String pwdS1234 = inputStream.readUTF();
		    Billing billing = new Billing();
		    billing.FromAccount = UUID.fromString(fromac);
		    billing.ToAccount = UUID.fromString(toacS);
		    billing.Balance = balD;
		    oc = Bank.getBank().Transfer(billing, pwdS1234);
		    if (oc == OperatorCode.SUCCESS)
		    {
			outputStream.writeUTF("1");
			outputStream.flush();
			outputStream.writeUTF(billing.BillingIDUuid.toString());
			outputStream.flush();
		    }
		    else
		    {
			outputStream.writeUTF("0");
			outputStream.flush();
			outputStream.writeUTF(oc.toString());
			outputStream.flush();
		    }
		    break;
		case 23:
		    String peopleIdS=inputStream.readUTF();
		    ArrayList<String> accountsAl=new ArrayList<String>();
		    oc=Bank.getBank().GetAccountList(UUID.fromString(peopleIdS), accountsAl);
		    if(oc==OperatorCode.SUCCESS)
		    {
			outputStream.writeUTF(Integer.toString(accountsAl.size()));

			String aaa="";
			for(int i=0; i<accountsAl.size(); i++)
			{
			    aaa+=accountsAl.get(i).toString()+"|";
			}
			outputStream.writeUTF(aaa);
		    }
		case 30:    //获取商店列表
		    ArrayList<Company> lists = new ArrayList<Company>();
		    oc = Market.getMarket().ListAll(lists);
		    if (oc == OperatorCode.SUCCESS)
		    {
			outputStream.writeUTF("1");
			outputStream.flush();
			outputStream.writeUTF(Integer.toString(lists.size()));
			outputStream.flush();
			String s = "";
			for (int i = 0; i < lists.size(); i++)
			{
			    Company cmpC = lists.get(i);
			    s += cmpC.IdUuid.toString() + "|";
			    s += cmpC.NameS + "|";
			    s += cmpC.Owner.PeopleIdUuid + "|";
			    s += cmpC.accountIdUuid.toString() + "|";
			    s += cmpC.DescriptionS + "|";
			}

			System.out.println(s);
			outputStream.writeUTF(s);
			outputStream.flush();
		    }
		    else
		    {
			outputStream.writeUTF("0");
			outputStream.flush();
		    }
		    break;
		case 31:    //商店物品列表
		    String idS1 = inputStream.readUTF();
		    Shop shop = new Shop(UUID.fromString(idS1));
		    ArrayList<Goods> goods = new ArrayList<Goods>();
		    oc = shop.ListAll(goods);
		    if (oc == OperatorCode.SUCCESS)
		    {
			outputStream.writeUTF("1");
			outputStream.flush();
			outputStream.writeUTF(Integer.toString(goods.size()));
			outputStream.flush();
			String s = "";
			for (int i = 0; i < goods.size(); i++)
			{
			    Goods good = goods.get(i);
			    s += good.IdUuid.toString() + "|";
			    s += good.NameS + "|";
			    s += Double.toString(good.SaleMoneyD) + "|";
			}
			outputStream.writeUTF(s);
			outputStream.flush();
		    }
		    else
		    {
			outputStream.writeUTF("0");
			outputStream.flush();
		    }
		    break;
		case 32:    //申请开店
		    String aS = inputStream.readUTF();
		    String[] asxSes = aS.split("\\|");
		    Company cmdC = new Company();
		    cmdC.NameS = asxSes[0];
		    cmdC.Owner = Community.getCommunity().GetPeopleInfo(UUID.fromString(asxSes[1]));
		    cmdC.accountIdUuid = UUID.fromString(asxSes[2]);
		    cmdC.pwd = asxSes[3];
		    cmdC.DescriptionS = asxSes[4];
		    oc = Market.getMarket().ApplyForNewShop(cmdC);
		    if (oc == OperatorCode.SUCCESS)
		    {
			outputStream.writeUTF("1");
		    }
		    else
		    {
			outputStream.writeUTF("0");
		    }
		    outputStream.flush();
		    break;
		case 33: //添加货物
		    String asS = inputStream.readUTF();
		    String[] asddSes = asS.split("\\|");
		    Goods goodss = new Goods();
		    goodss.NameS = asddSes[0];
		    goodss.SaleMoneyD = Double.valueOf(asddSes[1]);
		    goodss.CountI = Integer.parseInt(asddSes[2]);
		    goodss.CostsD = Double.valueOf(asddSes[3]);

		    ShopControl control = new ShopControl(UUID.fromString(asddSes[4]));
		    oc = control.Add(goodss);
		    if (oc == OperatorCode.SUCCESS)
		    {
			outputStream.writeUTF("1");
		    }
		    else
		    {
			outputStream.writeUTF("0");
		    }
		    outputStream.flush();
		    break;
		case 40:    //饭店初始化
		    ArrayList<Dish> dishes = new ArrayList<Dish>();
		    oc = Restaurant.getRestaurant().ListAll(dishes);
		    if (oc == OperatorCode.SUCCESS)
		    {
			Company cmy = Restaurant.getRestaurant().GetBusinessInfo();
			String s = "";
			s += cmy.IdUuid + "|" + cmy.NameS + "|" + cmy.Owner.NameS + "|" + cmy.accountIdUuid.toString() + "|" + cmy.DescriptionS + "|";
			outputStream.writeUTF("1");
			outputStream.flush();
			outputStream.writeUTF(Integer.toString(dishes.size()));
			outputStream.flush();

			for (int i = 0; i < dishes.size(); i++)
			{
			    Dish good = dishes.get(i);
			    s += good.IdUuid.toString() + "|";
			    s += good.NameS + "|";
			    s += Double.toString(good.SaleMoneyD) + "|";
			}
			outputStream.writeUTF(s);
			System.out.println(s);
			outputStream.flush();
		    }
		    else
		    {
			outputStream.writeUTF("0");
			outputStream.flush();
		    }
		    break;
		case 50: //软件公司初始化
		    break;
		case 51:    //软件销售初始化
		    ArrayList<SoftwareInfo> soft = new ArrayList<SoftwareInfo>();
		    oc = SoftSale.getSoftsale().ListAll(soft);
		    if (oc == OperatorCode.SUCCESS)
		    {
			Company cmy = SoftSale.getSoftsale().GetBusinessInfo();
			outputStream.writeUTF("1");
			outputStream.flush();
			String s = "";
			s += cmy.IdUuid + "|" + cmy.NameS + "|" + cmy.Owner.NameS + "|" + cmy.accountIdUuid.toString() + "|" + cmy.DescriptionS + "|";
			outputStream.writeUTF(Integer.toString(soft.size()));
			outputStream.flush();

			for (int i = 0; i < soft.size(); i++)
			{
			    SoftwareInfo good = soft.get(i);
			    s += good.IdUuid.toString() + "|";
			    s += good.NameS + "|";
			    s += Double.toString(good.SaleMoneyD) + "|";
			}
			outputStream.writeUTF(s);
			System.out.println(s);
			outputStream.flush();
		    }
		    else
		    {
			outputStream.writeUTF("0");
			outputStream.flush();
		    }
		    break;

	    }
	}
	catch (Exception e)
	{
	}
	finally
	{
	    try
	    {
		inputStream.close();
		outputStream.close();
		socket.close();
	    }
	    catch (IOException ex)
	    {
		Logger.getLogger(VzoneWorker.class.getName()).log(Level.SEVERE, null, ex);
	    }
	}
    }
}
